package com.suns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.suns.dao.AdminDao;
import com.suns.domain.Admin;
import com.suns.domain.User;
import com.suns.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao adminDao;
    @Override
    public Admin adminlogin(String tel, String pwd) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("tel", tel);
        Admin admin = adminDao.selectOne(wrapper);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return admin != null && passwordEncoder.matches(pwd, admin.getPwd()) ? admin : null;
    }

    @Override
    public PageInfo<Admin> getPageAdmins(int i, int pageSize, String keywords) {
        // 使用 PageHelper 进行分页操作
        PageHelper.startPage(i, pageSize);
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<Admin>();
        if(!Objects.equals(keywords, "null")){
            String likeKeywords = "%" + keywords.toLowerCase() + "%";
            wrapper.like(Admin::getId,likeKeywords)
                    .or().like(Admin::getName,likeKeywords)
                    .or().like(Admin::getAge,likeKeywords)
                    .or().like(Admin::getTel,likeKeywords)
                    .or().like(Admin::getGender,likeKeywords)
                    .or().like(Admin::getTime,likeKeywords);
        }
        List<Admin> adminList = adminDao.selectList(wrapper);
        return new PageInfo<>(adminList);
    }

    @Override
    public boolean updateAdmin(String name, String tel, String pwd, Integer age, String gender, String Npwd) {
        if(adminlogin(tel,pwd)!=null){
            String hashedPassword = null;
            if(Npwd!=null){
                BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
                 hashedPassword = passwordEncoder.encode(Npwd);
            }
            LambdaUpdateWrapper<Admin> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Admin::getTel, tel)
                    .set(Admin::getName,name)
                    .set(Admin::getAge,age)
                    .set(Admin::getGender,gender);
            if(hashedPassword!=null){
                wrapper.set(Admin::getPwd,hashedPassword);
            }
            // 执行更新操作
            int updateCount = adminDao.update(null, wrapper);
            return updateCount > 0;
        }
        return false;
    }

//    public boolean sign(String tel, String pwd) {
//        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
//        wrapper.eq("tel", tel);
//        if (adminDao.selectOne(wrapper) == null) {
//            Admin admin = new Admin();
//            admin.setTel(tel);
//            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//            String hashedPassword = passwordEncoder.encode(pwd);
//            admin.setPwd(hashedPassword);
//            return adminDao.insert(admin) > 0;
//        } else {
//            return false;
//        }
//    }
}
